1 package org.apache.maven.surefire.common.junit48;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.group.match.GroupMatcher;
23 import org.apache.maven.surefire.group.parse.GroupMatcherParser;
24 import org.apache.maven.surefire.group.parse.ParseException;
25 import org.apache.maven.surefire.testset.TestListResolver;
26 import org.junit.runner.manipulation.Filter;
27
28 import java.util.Map;
29 import java.util.Set;
30
31 import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
32 import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
33 import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank;
34
35
36
37
38 public class FilterFactory
39 {
40 private final ClassLoader testClassLoader;
41
42 public FilterFactory( ClassLoader testClassLoader )
43 {
44 this.testClassLoader = testClassLoader;
45 }
46
47
48
49
50
51
52 public boolean canCreateGroupFilter( Map<String, String> providerProperties )
53 {
54 String groups = providerProperties.get( TESTNG_GROUPS_PROP );
55 String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
56 return isNotBlank( groups ) || isNotBlank( excludedGroups );
57 }
58
59
60
61
62
63
64 public Filter createGroupFilter( Map<String, String> providerProperties )
65 {
66 String groups = providerProperties.get( TESTNG_GROUPS_PROP );
67
68 GroupMatcher included = null;
69 if ( isNotBlank( groups ) )
70 {
71 try
72 {
73 included = new GroupMatcherParser( groups ).parse();
74 }
75 catch ( ParseException e )
76 {
77 throw new IllegalArgumentException(
78 "Invalid group expression: '" + groups + "'. Reason: " + e.getMessage(), e );
79 }
80 }
81
82 String excludedGroups = providerProperties.get( TESTNG_EXCLUDEDGROUPS_PROP );
83
84 GroupMatcher excluded = null;
85 if ( isNotBlank( excludedGroups ) )
86 {
87 try
88 {
89 excluded = new GroupMatcherParser( excludedGroups ).parse();
90 }
91 catch ( ParseException e )
92 {
93 throw new IllegalArgumentException(
94 "Invalid group expression: '" + excludedGroups + "'. Reason: " + e.getMessage(), e );
95 }
96 }
97
98 if ( included != null && testClassLoader != null )
99 {
100 included.loadGroupClasses( testClassLoader );
101 }
102
103 if ( excluded != null && testClassLoader != null )
104 {
105 excluded.loadGroupClasses( testClassLoader );
106 }
107
108 return new GroupMatcherCategoryFilter( included, excluded );
109 }
110
111 public Filter createMethodFilter( String requestedTestMethod )
112 {
113 return new MethodFilter( requestedTestMethod );
114 }
115
116 public Filter createMethodFilter( TestListResolver resolver )
117 {
118 return new MethodFilter( resolver );
119 }
120
121 public Filter createFailingMethodFilter( Map<Class<?>, Set<String>> failingClassMethodMap )
122 {
123 return new FailingMethodFilter( failingClassMethodMap );
124 }
125
126 public Filter and( Filter filter1, Filter filter2 )
127 {
128 return new AndFilter( filter1, filter2 );
129 }
130 }